import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from "@/views/LoginView"
import RegistryView from "@/views/RegistryView"
import ProcessList from "@/views/ProcessList"
import DefaultData from "@/views/DefaultData"
import DemandInfo from "@/views/DemandInfo";
import GuestInfo from "@/views/GuestInfo";
import InstanceList from "@/views/InstanceList";
import PublicResource from "@/views/PublicResource";
import TodoTask from "@/views/TodoTask";
import UserList from "@/views/UserList";
import UserUpdate from "@/views/UserUpdate";
import HistoryDemand from "@/views/HistoryDemand";
import ProjectApproval from "@/views/ProjectApproval";
import NodePlan from "@/views/NodePlan";
import DesignView from "@/views/DesiginView";
import ElectricalView from "@/views/ElectricalView"
import Manu from "@/views/Manu"
import Review from "@/views/Review"
import ReviewDetail from "@/views/ReviewDetail"
import WMS from "@/views/WMS"
import BPMN from "@/views/BPMN"
import WarehousingForm from "@/views/WarehousingForm"
import ReviewhousingForm from "@/views/ReviewhousingForm"
import AddMaterialItem from "@/views/AddMaterialItem"
import AcceptanceWarehousingForm from "@/views/AcceptanceWarehousingForm"
import OutBound from "@/views/OutBound"
import ReviewOutbound from "@/views/ReviewOutbound"
import ExecuteOutboundForm from "@/views/ExecuteOutboundForm"
import ReleaseTasks from "@/views/ReleaseTasks"
import PrintWarehousing from "@/views/PrintWarehousing"
import PrintOutbound from "@/views/PrintOutbound";
import TaskToolTip from "@/views/TaskToolTip"
import SubTask from "@/views/SubTask";
import {TASK} from "bpmn-js/lib/features/replace/ReplaceOptions";
import ToolPurchase from "@/views/ToolPurchase";
import OrderList from "@/views/OrderList"
import CreateToolOrder from "@/views/CreateToolOrder";
import ToolFile from "@/views/ToolFile";
import ToolProductionType from "@/views/ToolProductionType";
import DelayApproval from "@/views/DelayApproval";
import ProductionManagement from "@/views/ProductionManagement";
import GMApprovalToolOrder from "@/views/GMApprovalToolOrder";
import ProductionProcess from "@/views/ProductionProcess"
import MApprovalToolOrder from "@/views/MApprovalToolOrder";
import ToolProductionPlan from "@/views/ToolProductionPlan"
import ToolDrawingData from "@/views/ToolDrawingData"
import IInitiateProcess from "@/views/IInitiateProcess"
import MaterialEdit from "@/views/MaterialEdit"
import Meeting from "@/views/Meeting";
import CreateMeeting from "@/views/CreateMeeting";
import MeetingDetail from "@/views/MeetingDetail";
import ReleaseMeetingTasks from "@/views/ReleaseMeetingTasks"
import PrepareMeetingRecord from "@/views/PrepareMeetingRecord"
import KPIList from "@/views/KPIList";
import KPIDetail from "@/views/KPIDetail"
import SchemeApproval from "@/views/SchemeApproval"
import SchemeUpdate from "@/views/SchemeUpdate";
import KPIItemScheme from "@/views/KPIItemScheme"
import EditKPI from "@/views/EditKPI"
import ModifyOutbound from "@/views/ModifyOutbound";
import WIP from "@/views/WIP"
import BusinessData from "@/views/BusinessData"
import GMApprovalPartyTask from "@/views/GMApprovalPartyTask"

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView,
        redirect: '/default_data',
        children: [
            {
                path: '/default_data',
                name: 'DefaultData',
                component: () => import('../views/DefaultData')
            },
            {
                path: '/process_list',
                name: 'ProcessList',
                component: () => import('../views/ProcessList')
            },
            {
                path: '/instance_list',
                name: 'InstanceList',
                component: () => import('../views/InstanceList')
            },
            {
                path: '/todo_task',
                name: 'TodoTask',
                component: () => import('../views/TodoTask')
            },
            {
                path: '/history_task',
                name: 'HistoryTask',
                component: () => import('../views/HistoryTask')
            },

            {
                path: '/guest_info',
                name: 'GuestInfo',
                component: () => import('../views/GuestInfo')
            },
            {
                path: '/demand_info',
                name: 'DemandInfo',
                component: () => import('../views/DemandInfo')
            },
            {
                path: '/public_resource',
                name: 'PublicResource',
                component: () => import('../views/PublicResource')
            },
            {
                path: '/user_list',
                name: 'UserList',
                component: () => import('../views/UserList')
            },
            {
                path: '/user_update',
                name: 'UserUpdate',
                component: () => import('../views/UserUpdate')
            },
            {
                path: '/history_demand',
                name: 'HistoryDemand',
                component: () => import('../views/HistoryDemand')
            },
            {
                path: '/project_aproval',
                name: 'ProjectApproval',
                component: () => import('../views/ProjectApproval')
            },
            {
                path: '/node_plan',
                name: 'NodePlan',
                component: () => import('../views/NodePlan')
            }
            ,
            {
                path: '/eq_te',
                name: 'EquipmetTech',
                component: () => import('../views/EquipmentTech')
            },
            {
                path: '/design',
                name: 'DesignView',
                component: () => import('../views/DesiginView')
            },
            {
                path: '/electrical',
                name: 'ElectricalView',
                component: () => import('../views/ElectricalView')
            },
            {
                path: '/manu',
                name: 'Manu',
                component: () => import('../views/Manu')
            },
            {
                path: '/review',
                name: 'Review',
                component: () => import('../views/Review')
            },
            {
                path: '/review_detail',
                name: 'ReviewDetail',
                component: () => import('../views/ReviewDetail')
            },

            {
                path: '/wms',
                name: 'WMS',
                component: () => import('../views/WMS')
            },
            {
                path: '/task',
                name: 'Task',
                component: () => import('../views/Task')
            },
            {
                path: '/bpmn',
                name: 'BPMN',
                component: () => import('../views/BPMN')
            },
            {
                path: '/warehousing_form',
                name: 'WarehousingForm',
                component: () => import('../views/WarehousingForm')
            },
            {
                path: '/reviewhousing_form',
                name: 'ReviewhousingForm',
                component: () => import('../views/ReviewhousingForm')
            },
            {
                path: '/add_material_item',
                name: 'AddMaterialItem',
                component: () => import('../views/AddMaterialItem')
            },
            {
                path: '/acceptance_warehousing_form',
                name: 'AcceptanceWarehousingForm',
                component: () => import('../views/AcceptanceWarehousingForm')
            },
            {
                path: '/outbound',
                name: 'OutBound',
                component: () => import('../views/OutBound')
            },
            {
                path: '/review_outbound',
                name: 'ReviewOutbound',
                component: () => import('../views/ReviewOutbound')
            },
            {
                path: '/execute_outbound_form',
                name: 'ExecuteOutboundForm',
                component: () => import('../views/ExecuteOutboundForm')
            },
            {
                path: '/material_records',
                name: 'MaterialRecords',
                component: () => import('../views/MaterialRecords')
            },
            {
                path: '/print_warehousing',
                name: 'PrintWarehousing',
                component: () => import('../views/PrintWarehousing')
            },
            {
                path: '/print_outbound',
                name: 'PrintOutbound',
                component: () => import('../views/PrintOutbound')
            },
            {
                path: '/release_tasks',
                name: 'ReleaseTasks',
                component: () => import('../views/ReleaseTasks')
            },
            {
                path: '/approval_tasks',
                name: 'ApprovalTasks',
                component: () => import('../views/ApprovalTasks')
            },
            {
                path: '/task_tool_tip',
                name: 'TaskToolTip',
                component: () => import ('../views/TaskToolTip')
            },
            {
                path: '/sub_task',
                name: 'SubTask',
                component: () => import ('../views/SubTask')
            },
            {
                path: '/delay_approval',
                name: 'DelayApproval',
                component: () => import ('../views/DelayApproval')
            },
            {
                path: '/gantt',
                name: 'TaskReport1',
                component: () => import ('../views/TaskReport1')
            },
            {
                path: '/order_list',
                name: 'OrderList',
                component: () => import ('../views/OrderList')
            },
            {
                path: '/create_tool_order',
                name: 'CreateToolOrder',
                component: () => import ('../views/CreateToolOrder')
            },
            {
                path: '/tool_production_type',
                name: 'ToolProductionType',
                component: () => import ('../views/ToolProductionType')
            },
            {
                path: '/tool_purchase',
                name: 'ToolPurchase',
                component: () => import ('../views/ToolPurchase')
            },
            {
                path: '/tool_file',
                name: 'ToolFile',
                component: () => import ('../views/ToolFile')
            },
            {
                path: '/production_management',
                name: 'ProductionManagement',
                component: () => import('../views/ProductionManagement')
            },
            {
                path: '/gm_approval_tool_order',
                name: 'GMApprovalToolOrder',
                component: () => import('../views/GMApprovalToolOrder')
            },
            {
                path: '/production_process',
                name: 'ProductionProcess',
                component: () => import('../views/ProductionProcess')
            },
            {
                path: '/mapproval_tool_order',
                name: 'MApprovalToolOrder',
                component: () => import('../views/MApprovalToolOrder')
            },
            {
                path: '/tool_production_plan',
                name: 'ToolProductionPlan',
                component: () => import('../views/ToolProductionPlan')
            },
            {
                path: '/tool_drawing_data',
                name: 'ToolDrawingData',
                component: () => import('../views/ToolDrawingData')
            },
            {
                path: '/i_initiate_process',
                name: 'IInitiateProcess',
                component: () => import('../views/IInitiateProcess')
            },
            {
                path: '/material_edit',
                name: 'MaterialEdit',
                component: () => import('../views/MaterialEdit')
            },
            {
                path: '/meeting',
                name: 'Meeting',
                component: () => import('../views/Meeting')
            },
            {
                path: '/prepare_meeting_record',
                name: 'PrepareMeetingRecord',
                component: () => import('../views/PrepareMeetingRecord')
            },
            {
                path: '/create_meeting',
                name: 'CreateMeeting',
                component: () => import('../views/CreateMeeting')
            },
            {
                path: '/meeting_detail',
                name: 'MeetingDetail',
                component: () => import('../views/MeetingDetail')
            },
            {
                path: '/release_meeting_tasks',
                name: 'ReleaseMeetingTasks',
                component: () => import('../views/ReleaseMeetingTasks')
            },
            {
                path: '/kpi_list',
                name: 'KPIList',
                component: () => import('../views/KPIList')
            },
            {
                path: '/kpi_detail',
                name: 'KPIDetail',
                component: () => import('../views/KPIDetail')
            },
            {
                path: '/scheme_approval',
                name: 'SchemeApproval',
                component: () => import('../views/SchemeApproval')
            },
            {
                path: '/scheme_update',
                name: 'SchemeUpdate',
                component: () => import('../views/SchemeUpdate')
            },
            {
                path: '/kpi_item_scheme',
                name: 'KPIItemScheme',
                component: () => import('../views/KPIItemScheme')
            },
            {
                path: '/edit_kpi',
                name: 'EditKPI',
                component: () => import('../views/EditKPI')
            },
            {
                path: '/modify_outbound',
                name: 'ModifyOutbound',
                component: () => import('../views/ModifyOutbound')
            },
            {
                path: '/wip',
                name: 'WIP',
                component: () => import('../views/WIP')
            },
            {
                path: '/business_data',
                name: 'BusinessData',
                component: () => import('../views/BusinessData')
            },
            {
                path: '/gm_approval_party_task',
                name: 'GMApprovalPartyTask',
                component: () => import('../views/GMApprovalPartyTask')
            },
        ]
    },
    {
        path: '/login',
        name: 'LoginView',
        component: LoginView
    },
    {
        path: '/registry',
        name: 'RegistryView',
        component: RegistryView
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    if (!isAuthenticated() && to.path !== '/login' && to.path !== '/registry') {
        // 用户未登录且不是前往登录或注册页面
        next('/login'); // 重定向到登录页面
    } else {
        // 用户已登录，或者是前往登录、注册页面
        next(); // 正常导航到目标页面
    }
});

function isAuthenticated() {
    // 尝试从localStorage获取用户数据
    const userData = localStorage.getItem('user');
    if (!userData) {
        return false; // 如果没有用户数据，直接返回未认证
    }

    // 解析存储的字符串为JSON对象
    const user = JSON.parse(userData);

    // 检查用户的账户状态
    return user.enabled &&
        user.accountNonExpired &&
        user.credentialsNonExpired &&
        user.accountNonLocked;
}

export default router
